var app = getApp();
import ajaxhelper from '../../utils/ajaxhelper.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList: [],//列表数据
    hasRefresh: false,
    isGetMore: false,
    hasMore: true,
    pageSize: 10,
    pageNo: 1,
    isShow: false,//是否显示没有列表的提示图
    time: ''
  },

  onLoad: function (option) {
    console.log(option)
    this.getListData(); //获取列表数据
  },

  //列表渲染数据
  getListData: function () {
    let param = {}
    var token = wx.getStorageSync('Token_date') && wx.getStorageSync('Token_date').Token;
    ajaxhelper.post(app.globalData.frontJSHost + "knowledge/myPaid/" + this.data.pageNo + "/" + this.data.pageSize +"?token="+encodeURIComponent(token), param, this, this.setListData)
  },

  setListData(res) {
    console.log(res)
    if (res.result && res.data) {

      var Data = res.data;
      console.log(Data)
      var result = Data.result || '';
      var length = result.length;
      console.log(length)
      var pageNo = this.data.pageNo;
      var hasmore = Data.totalCount - pageNo * this.data.pageSize;

      this.setData({
        pageNo: ++pageNo,
        hasRefresh: false,
        isGetMore: false,
        hasMore: hasmore
        // tip: '上拉加载更多数据'
      })

      for (var i = 0; i < length; i++) {

        var gmtCreate = result[i].gmtCreate;
        var time = new Date(gmtCreate).format("yyyy-MM-dd");
        var mediaType = result[i].mediaType;
        if (mediaType == 'AUDIO') {
          mediaType = '音频'
        } else {
          mediaType = '视频'
        }
        console.log(time)
        result[i].time = time;
        result[i].mediaType = mediaType;
      }

      if (Data && result) {
        this.setData({
          dataList: this.data.dataList.concat(result),
        })
      }

    } else {
      this.setData({
        hasRefresh: false,
        isGetMore: false,
      })
      wx.showToast({
        title: !!res.message.global ? res.message.global : "请求出错",
        icon: "loading"
      })
    }
    this.setData({
      isShow: true
    })
  }, 

  // toalbum: function (e) {
  //   var id = e.currentTarget.dataset.id;
  //   console.log(id)
  //   var type = e.currentTarget.dataset.type;
  //   wx.navigateTo({
  //     url: '../payment/payment?id=' + id
  //   })
  // },

  //下拉刷新
  onPullDownRefresh() {
    this.setData({
      isShow: false
    })
    if (this.data.hasRefresh) return false;
    this.setData({
      dataList: [],
      hasRefresh: true,
      pageNo: 1
    })
    this.getListData(this.data.currentTab)
    setTimeout(function () {
      wx.stopPullDownRefresh()
    }, 1400);
  },
  //上拉加载
  onReachBottom() {
    if (this.data.isGetMore) return false;
    if (this.data.hasRefresh) return false;
    if (this.data.hasMore > 0) {
      this.setData({
        isGetMore: true,
        // tip: '正在加载中...'
      })
      this.getListData(this.data.currentTab);
    } else {
      this.setData({
        isGetMore: false,
        // tip: '没有更多数据了...'
      })
      return false
    }
  }
})